# coding: utf-8
import os
import sys
import logging
import pygame

try:
    __file__
except NameError as e:
    from whereis import mymain
    rootdir = mymain()
else:
    rootdir = os.path.dirname(os.path.dirname(__file__))

sys.path.insert(0, rootdir)
from lib.bashlog import getlogger
from lib.tracemore import get_exc_plus
from lib.config import Config
from lib.data import Data
from lib import color
from lib import events
from lib.mainpage import MainPage
from lib.scrollpage import CategoryPage
from lib.scrollpage import LanguagePage
sys.path.pop(0)

logger = logging.getLogger('wordson.game')

cfg = Config()
data = Data()

class Game(object):
    _cate_page = None
    def __init__(self):
        pygame.init()

        self.main_page = MainPage()
        self.lang_page = LanguagePage(data.get_langs())
        self.load_page = None
        self.game_page = None

        self.current_page = self.main_page

    @property
    def cate_page(self):
        if self._cate_page is None:
            self._cate_page = CategoryPage(data.get_cates_under(self.lang))
        return self._cate_page



    def run(self):

        pygame.key.set_repeat(cfg.key_repeat_delay, cfg.key_repeat_interval)
        pygame.display.set_caption(cfg.title)

        screen = pygame.display.set_mode(cfg.screen)

        run = True

        clock = pygame.time.Clock()

        while run:
            for event in pygame.event.get():
                if self.current_page.handle(event):
                    continue
                if self.handle(event):
                    continue
                if event.type == pygame.QUIT:
                    logger.debug('exit...')
                    run = False
                    break

            interval = clock.tick(cfg.tick)
            self.current_page.update(interval)
            screen.fill(color.white)
            self.current_page.draw(screen)
            pygame.display.flip()

        pygame.quit()

    def handle(self, event):
        if event.type == events.SWITCHPAGE:
            to = event.to
            if to == cfg.page_main:
                self.current_page = self.main_page
                # if hasattr(self, 'lang'):
                #     del self.lang
            elif to == cfg.page_lang:
                self.current_page = self.lang_page
                if hasattr(self, 'lang'):
                    del self.lang
            elif to == cfg.page_cate:
                self.lang = event.lang
                self.current_page = self.cate_page
            elif to == cfg.page_load:
                self.current_page = self.load_page
            elif to == cfg.page_game:
                lang = self.lang
                cate = event.cate
                logger.debug('switch to game %s - %s', lang, cate)
                self.current_page = self.game_page
            logger.debug('event %s handle by game', event)
            return True


if __name__ == '__main__':
    getlogger('wordson', logging.DEBUG)
    game = Game()
    game.run()
